package com.potenza.goods.controller;

import com.potenza.goods.auth.sys.feign.ShiroServiceFeignClient;
import com.potenza.goods.entity.GoodsEntity;
import com.potenza.goods.service.GoodsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 商品管理
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-01-05 16:39:00
 */
@RestController
//@CrossOrigin(value = "http://localhost:8001",allowCredentials="true")
@RequestMapping("goods/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    ShiroServiceFeignClient shiroServiceFeignClient;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:goods:list")
    public R list(@RequestParam Map<String, Object> params)  {
        SysUserEntity entity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();

        /*Random random = new Random();
        int i = random.nextInt(100);
        if(i%2==0){
            throw new RuntimeException("2222");
        }*/
        System.out.println(entity);
        PageUtils page = goodsService.queryPage(params);

        SysUserEntity sysUserEntity = shiroServiceFeignClient.queryUser(1L);

        System.out.println(sysUserEntity);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}")
    @RequiresPermissions("goods:goods:info")
    public R info(@PathVariable("goodsId") Long goodsId){
		GoodsEntity goods = goodsService.getById(goodsId);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:goods:save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:goods:update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:goods:delete")
    public R delete(@RequestBody Long[] goodsIds){
		goodsService.removeByIds(Arrays.asList(goodsIds));

        return R.ok();
    }

}
